﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

#include "IInteractionUiManipulateManager.h"
#include "UniIdentity.h"

namespace gcmp
{
    template<class ElementClass, class UiManipulateStrategyClass>
    class ElementUiManipulateStrategyRegister
    {
    public:
        ElementUiManipulateStrategyRegister()
        {
            auto* pMgr = IInteractionUiManipulateManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pMgr, L"pMgr为空", L"GMEP", L"2020-12-18");

            const UniIdentity& implUid = ElementClass::GetImplementationUid();
            IElementUiManipulateStrategy* pUiManipulateStrategy = pMgr->GetStrategy(implUid);
            if (!pUiManipulateStrategy)
            {
                OwnerPtr<IElementUiManipulateStrategy> opUiManipulateStrategy = NEW_AS_OWNER_PTR(UiManipulateStrategyClass);
                pMgr->AddStrategy(implUid, TransferOwnership(opUiManipulateStrategy));
            }
        }

        ~ElementUiManipulateStrategyRegister()
        {
        }
    };
}

#define REGISTER_ELEMENT_UIMANIPULATE_STRATEGY(ELEMENT_CLASS, UIMANIPULATE_STRATEGY_CLASS) \
namespace { \
    static gcmp::ElementUiManipulateStrategyRegister<ELEMENT_CLASS, UIMANIPULATE_STRATEGY_CLASS> \
        s_##ELEMENT_CLASS##UIMANIPULATE_STRATEGY_CLASS##ElementUiManipulateStrategyRegister; \
}

